package com.example.sprintboot.controller;

import com.example.sprintboot.service.OrderService;
import com.example.sprintboot.service.ProductService;
import com.example.sprintboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/dashboard")
@PreAuthorize("hasRole('ADMIN')")
public class AdminDashboardController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    // 获取数据看板统计信息
    @GetMapping("/summary")
    public Map<String, Object> getSummary() {
        try {
            Map<String, Object> summary = new HashMap<>();
            
            // 获取订单统计数据
            Map<String, Object> orderStats = orderService.getAdminOrderStats();
            
            // 平台总销售额
            summary.put("totalSales", orderStats.get("totalRevenue"));
            
            // 订单数
            summary.put("orderCount", orderStats.get("totalOrders"));
            
            // 用户数（所有用户）
            summary.put("userCount", userService.selectAllUsers().size());
            
            // 商品数（所有商品）
            summary.put("productCount", productService.getProductCountBySellerId(null));
            
            return summary;
        } catch (Exception e) {
            Map<String, Object> error = new HashMap<>();
            error.put("error", "获取统计数据失败: " + e.getMessage());
            return error;
        }
    }
} 